using System;
using System.Collections;
using System.Collections.Generic;
using System.Html;
using Awaiter;
using jQueryApi;

namespace Nsb.Classes
{
    public partial class Truple : Actor
    {
        public static void ScoopDataTo(object data)
        {
            jQueryObject input = jQuery.Select("body .Truple .Input");

            input.Each(
                delegate
                {
                    string key = (string) jQuery.This.GetDataValue("fieldName");
                    object value = jQuery.This.Plugin<Jewl>().GetFValue();
                    Type.SetField(data, key, value);
                    return true;
                }
                );
        }

        public static void FillDataFromAw(Await awp, object data)
        {
            new Await()
                .WaitDx(delegate { return jQuery.Select("body .Truple .Input").Length > 0; }, 500)
                .AddDx(delegate { FillDataFrom((Dictionary) data); })
                .Commit(awp);
        }

        public static void FillDataFrom(Dictionary data)
        {
            jQueryObject input = jQuery.Select("body .Truple .Input");

            input.Each(
                delegate
                {
                    string key = (string) jQuery.This.GetDataValue("fieldName");
                    if (data.ContainsKey(key))
                    {
                        object value = data[key];
                        jQuery.This.Plugin<Jewl>().FValue(value);
                    }
                    return true;
                }
                );
        }

        public static void EachTruple(ElementInterruptibleIterationCallback fn)
        {
            jQueryObject input = jQuery.Select("body .Truple");
            input.Each(fn);
        }

        public static void EachInput(ElementInterruptibleIterationCallback fn)
        {
            jQueryObject input = jQuery.Select("body .Truple .Input");
            input.Each(fn);
        }

        public void SetOptions(Dictionary options)
        {
            foreach (KeyValuePair<string, object> p in options)
            {
                switch (p.Key)
                {
                    case "Validate":
                        break;
                    case "Label":
                        SubHtml(".Label", p.Value.ToString());
                        break;
                    case "Value":
                        SubValue(".Input", p.Value.ToString());
                        break;
                    default:
                        SubAttr(".Input", p.Key, p.Value.ToString());
                        break;
                }
            }
        }

        public Truple FollowFocus()
        {
            Element
                .FocusIn(
                    delegate(jQueryEvent e)
                    {
                        jQuery.FromElement(e.Target).AddClass("Focused");
                        GetTruple(e.Target).Element.AddClass("Focused");
                    })
                .FocusOut(
                    delegate(jQueryEvent e)
                    {
                        jQuery.FromElement(e.Target).RemoveClass("Focused");
                        GetTruple(e.Target).Element.RemoveClass("Focused");
                    })
                ;
            return this;
        }

        public void AddSlideTruples(Await awp)
        {
            EachTruple(
                delegate(int i, Element domEl)
                {
                    awp.AddAw(SlideInAw, jQuery.FromElement(domEl));
                    return true;
                })
                ;
        }

        public void SlideInByNameAw(Await awp, string name)
        {
            SlideInAw(awp, Truples[name].Element);
        }
    }
}